Part one
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 1.0.0
## ✓ tidyr 1.0.3 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
bison <- read_csv('https://raw.githubusercontent.com/brendanrbrown/brendanrbrown.github.io/master/smallbison.csv')
## Parsed with column specification:
## cols(
## latitude = col_double(),
## longitude = col_double(),
## dtime = col_double()
## )
K <- 6
set.seed(1305)
m <- kmeans(data.matrix(bison), centers = K)
bison <- mutate(bison, cluster = factor(m$cluster))
cent <- as_tibble(m$centers) %>% mutate(cluster = factor(1:K))
ggplot(bison, aes(x = latitude, y = longitude, color = cluster)) + geom_point(size = 3, alpha = .7) +
theme_bw() + scale_color_brewer(type = "qual", palette = "Set1") +
geom_point(data = cent, size = 10, shape = 13)

library(plotly) # https://plot.ly/r/getting-started/
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
plot_ly(data = bison, type = "scatter3d", mode = "markers", opacity = .5,
x = ~latitude, y = ~longitude, z = ~dtime, color = ~cluster)
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
Part 2
bison <- read_csv('https://raw.githubusercontent.com/brendanrbrown/brendanrbrown.github.io/master/smallbison.csv')
## Parsed with column specification:
## cols(
## latitude = col_double(),
## longitude = col_double(),
## dtime = col_double()
## )
K <- 4
set.seed(1305)
bison_s = data.matrix(bison) %>% scale
m <- kmeans(bison_s, centers = K)
bison <- as.tibble(bison_s) %>% mutate(cluster = factor(m$cluster))
## Warning: `as.tibble()` is deprecated as of tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
cent <- as_tibble(m$centers) %>% mutate(cluster = factor(1:K))
ggplot(bison, aes(x = latitude, y = longitude, color = cluster)) + geom_point(size = 3, alpha = .7) +
theme_bw() + scale_color_brewer(type = "qual", palette = "Set1") +
geom_point(data = cent, size = 10, shape = 13)
